package me.zhengjie.hr.employeeposition.service.impl;

import me.zhengjie.hr.employeeposition.domain.EmployeePosition;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.hr.employeeposition.repository.EmployeePositionRepository;
import me.zhengjie.hr.employeeposition.service.EmployeePositionService;
import me.zhengjie.hr.employeeposition.service.dto.EmployeePositionDto;
import me.zhengjie.hr.employeeposition.service.dto.EmployeePositionQueryCriteria;
import me.zhengjie.hr.employeeposition.service.mapper.EmployeePositionMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @author ZGZ
* @date 2020-04-15
*/
@Service
//@CacheConfig(cacheNames = "employeePosition")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class EmployeePositionServiceImpl implements EmployeePositionService {

    private final EmployeePositionRepository employeePositionRepository;

    private final EmployeePositionMapper employeePositionMapper;

    public EmployeePositionServiceImpl(EmployeePositionRepository employeePositionRepository, EmployeePositionMapper employeePositionMapper) {
        this.employeePositionRepository = employeePositionRepository;
        this.employeePositionMapper = employeePositionMapper;
    }

    @Override
    //@Cacheable
    public Map<String,Object> queryAll(EmployeePositionQueryCriteria criteria, Pageable pageable){
        Page<EmployeePosition> page = employeePositionRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(employeePositionMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<EmployeePositionDto> queryAll(EmployeePositionQueryCriteria criteria){
        return employeePositionMapper.toDto(employeePositionRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public EmployeePositionDto findById(Integer id) {
        EmployeePosition employeePosition = employeePositionRepository.findById(id).orElseGet(EmployeePosition::new);
        ValidationUtil.isNull(employeePosition.getId(),"EmployeePosition","id",id);
        return employeePositionMapper.toDto(employeePosition);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public EmployeePositionDto create(EmployeePosition resources) {
        return employeePositionMapper.toDto(employeePositionRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(EmployeePosition resources) {
        EmployeePosition employeePosition = employeePositionRepository.findById(resources.getId()).orElseGet(EmployeePosition::new);
        ValidationUtil.isNull( employeePosition.getId(),"EmployeePosition","id",resources.getId());
        employeePosition.copy(resources);
        employeePositionRepository.save(employeePosition);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            employeePositionRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<EmployeePositionDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (EmployeePositionDto employeePosition : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("职务id", employeePosition.getPositionId());
            map.put("人员id", employeePosition.getEmpId());
            map.put("开始时间", employeePosition.getStartTime());
            map.put("结束时间", employeePosition.getOverTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}